package com.yami.shop.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Data
@Component
public class DeviceConfig implements InitializingBean {
    private int basePrice;
    private List<AttributeConfig> attributes;

    @Override
    public void afterPropertiesSet() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        ClassPathResource resource = new ClassPathResource("device-config.json");
        try (InputStream inputStream = resource.getInputStream()) {
            DeviceConfig config = mapper.readValue(inputStream, DeviceConfig.class);
            this.basePrice = config.getBasePrice();
            this.attributes = config.getAttributes();
        }
    }




    @Data
    public static class AttributeConfig {
        private String name;
        private String type; // MULTI_SELECT or SINGLE_SELECT
        private List<OptionConfig> options;
    }

    @Data
    public static class OptionConfig {
        private String value;
        private Integer price;
    }
}
